/**
 * 
 */
package action.teacher;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;


import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
import model.*;
/** 
 * @作者 Zouzr 
 * @创建日期 Dec 19, 2008 
 * @版本 V 1.0 
 */

public class LoginAction extends ActionSupport 
{
	
	private String teacherusername;//
	
	private String teacherpassword;
	
	private String checkcode;
	
	private String message="";
	
	
	/**
	 * 修改密码的属性
	 */
	private String teacherpasswordold;//旧密码
	
	private String teacherpasswordnew;//新密码
	
	private String teacherpasswordchecknew;//确认新密码
	
	private ServletActionContext context;
	
	/**
	 * 登陆验证
	 * @return
	 */
	public String teacherLogin()
	{
		if(teacherusername.equals("")||teacherusername==null)
		{
			message=message+"用户名未填写<br>";
			return SUCCESS;
		}
		if(teacherpassword.equals("")||teacherpassword==null)
		{
			message=message+"密码未填写<br>";
			return SUCCESS;
		}
		if(checkcode.equals("")||checkcode==null)
		{
			message=message+"验证码未填写<br>";
			return SUCCESS;
		}
		
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        TeacherDAO teacherdao = (TeacherDAO)context.getBean("TeacherDAOProxy");
        String result=teacherdao.loginValidate(teacherusername, teacherpassword);
        if(result.equals("UserNotExist"))
        {
        	message=message+"用户不存在<br>";
			return SUCCESS;
        }
        if(result.equals("PasswordError"))
        {
        	message=message+"密码错误<br>";
			return SUCCESS;
        }
        Map map = ActionContext.getContext().getSession();
        String temp_checkcode = (String) map.get("randCode");
        if(!checkcode.equals(temp_checkcode))
		{
			message=message+"验证码错误<br>";
			return SUCCESS;
		}
        System.out.println("result---------"+result);
        String[] results = result.split(";");
        this.context.getRequest().getSession().setAttribute("teacherId", results[0]);
        this.context.getRequest().getSession().setAttribute("teacherusername", teacherusername);
        this.context.getRequest().getSession().setAttribute("teachername", results[1]);
        System.out.println("----------"+ this.context.getRequest().getSession().getAttribute("teacherusername"));
        return SUCCESS;
	}

	/**
	 * 退出
	 * @return
	 */
	public String teacherLogout()
	{
		this.context.getRequest().getSession().setAttribute("teacherId", "");
		this.context.getRequest().getSession().setAttribute("teacherusername", "");
        this.context.getRequest().getSession().setAttribute("teachername", "");
        System.out.println("退出后----------"+ this.context.getRequest().getSession().getAttribute("teacherusername"));
        System.out.println("退出后----------"+ this.context.getRequest().getSession().getAttribute("teachername"));
		return SUCCESS;
	}
	/**
	 * 修改密码
	 */
	public String teacherPasswordEdit()
	{
		if(teacherpasswordold.equals("")||teacherpasswordold==null)
		{
			message=message+"旧密码未填写<br>";
			return SUCCESS;
		}
		if(teacherpasswordnew.equals("")||teacherpasswordnew==null)
		{
			message=message+"新密码未填写<br>";
			return SUCCESS;
		}
		if(teacherpasswordchecknew.equals("")||teacherpasswordchecknew==null)
		{
			message=message+"确认新密码未填写<br>";
			return SUCCESS;
		}
		if(checkcode.equals("")||checkcode==null)
		{
			message=message+"验证码未填写<br>";
			return SUCCESS;
		}
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        TeacherDAO teacherdao = (TeacherDAO)context.getBean("TeacherDAOProxy");
        System.out.println("修改密码时的用户名为"+this.context.getRequest().getSession().getAttribute("teacherusername"));
        String result=teacherdao.loginValidate((String)this.context.getRequest().getSession().getAttribute("teacherusername"), teacherpasswordold);
        if(result.equals("PasswordError"))
        {
        	message=message+"密码错误<br>";
			return SUCCESS;
        }
        if(!teacherpasswordnew.equals(teacherpasswordchecknew))
        {
        	message=message+"新密码和确认密码必须一致<br>";
			return SUCCESS;
        }
        Map map = ActionContext.getContext().getSession();
        String temp_checkcode = (String) map.get("randCode");
        if(!checkcode.equals(temp_checkcode))
		{
			message=message+"验证码错误<br>";
			return SUCCESS;
		}
        teacherdao.editPassword((String)this.context.getRequest().getSession().getAttribute("teacherusername"), teacherpasswordnew);
		return SUCCESS;
	}
	
	/**
	 * @return the teacherusername
	 */
	public String getTeacherusername() {
		return teacherusername;
	}

	/**
	 * @param teacherusername the teacherusername to set
	 */
	public void setTeacherusername(String teacherusername) {
		this.teacherusername = teacherusername;
	}

	/**
	 * @return the teacherpassword
	 */
	public String getTeacherpassword() {
		return teacherpassword;
	}

	/**
	 * @param teacherpassword the teacherpassword to set
	 */
	public void setTeacherpassword(String teacherpassword) {
		this.teacherpassword = teacherpassword;
	}

	/**
	 * @return the checkcode
	 */
	public String getCheckcode() {
		return checkcode;
	}

	/**
	 * @param checkcode the checkcode to set
	 */
	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the teacherpasswordold
	 */
	public String getTeacherpasswordold() {
		return teacherpasswordold;
	}

	/**
	 * @param teacherpasswordold the teacherpasswordold to set
	 */
	public void setTeacherpasswordold(String teacherpasswordold) {
		this.teacherpasswordold = teacherpasswordold;
	}

	/**
	 * @return the teacherpasswordnew
	 */
	public String getTeacherpasswordnew() {
		return teacherpasswordnew;
	}

	/**
	 * @param teacherpasswordnew the teacherpasswordnew to set
	 */
	public void setTeacherpasswordnew(String teacherpasswordnew) {
		this.teacherpasswordnew = teacherpasswordnew;
	}

	/**
	 * @return the teacherpasswordchecknew
	 */
	public String getTeacherpasswordchecknew() {
		return teacherpasswordchecknew;
	}

	/**
	 * @param teacherpasswordchecknew the teacherpasswordchecknew to set
	 */
	public void setTeacherpasswordchecknew(String teacherpasswordchecknew) {
		this.teacherpasswordchecknew = teacherpasswordchecknew;
	}
}
